import { Component, OnInit, ViewEncapsulation, Injector, ViewChild } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { PiciServiceProxy, PiciDto, ModelServiceProxy, ComboboxItemDto } from '@shared/service-proxies/service-proxies';
import { LazyLoadEvent } from 'primeng/api';
import { AppComponentBase } from '@shared/common/app-component-base';
import { Table } from 'primeng/table';
import { Paginator } from 'primeng/primeng';

@Component({
    templateUrl: './pici.component.html',
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()]
})
export class PiciComponent extends AppComponentBase implements OnInit {
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;

    filterText = '';

    constructor(injector: Injector, private _piciService: PiciServiceProxy, private _modelService: ModelServiceProxy) {
        super(injector);
    }

    ngOnInit(): void {
        // this._modelService.getComboboxModels().subscribe(result => {
        //     this.models = result.items;
        // });
    }

    getPicis(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);

            return;
        }

        this.primengTableHelper.showLoadingIndicator();

        this._piciService
            .getAll(
                this.filterText,
                this.primengTableHelper.getSorting(this.dataTable),
                this.primengTableHelper.getMaxResultCount(this.paginator, event),
                this.primengTableHelper.getSkipCount(this.paginator, event)
            )
            .subscribe(result => {
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.hideLoadingIndicator();
            });
    }

    deletePici(pici: PiciDto): void {
        let self = this;
        self.message.confirm(self.l('RoleDeleteWarningMessage', pici.name), this.l('AreYouSure'), isConfirmed => {
            if (isConfirmed) {
                this._piciService.delete(pici.id).subscribe(() => {
                    this.getPicis();
                    abp.notify.success(this.l('SuccessfullyDeleted'));
                });
            }
        });
    }
}
